﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DeviceID
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        { 
            InitializeComponent();
            this.Loaded += MainWindow_Loaded;
        }
        public static string MD5Hash(string input)
        {
            MD5 md5 = MD5.Create();
            byte[] bs = Encoding.UTF8.GetBytes(input);
            bs = md5.ComputeHash(bs);
            StringBuilder s = new System.Text.StringBuilder();
            foreach (byte b in bs)
            {
                s.Append(b.ToString("x2").ToUpper());
            }
            return s.ToString();
        }
        private async void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            btn.IsEnabled = false;

            txt.Text = "正在读取...";
            try
            {

                var md5 = await Task.Run<string>(delegate
             {
                 var cpuid = SharpSoft.OS.Windows.GetCPUID();
                 var uuid = SharpSoft.OS.Windows.GetBaseBoardUUID();
                 var disk = SharpSoft.OS.Windows.GetHardDiskSerialNumber();
                 var id = MD5Hash($"{cpuid}{uuid}{disk}");
                 return id;
             });
                txt.Text = md5;
                btn.IsEnabled = true;
            }
            catch (Exception _)
            {
                txt.Text = "获取设备信息失败！";
                txt.Foreground = Brushes.Red;
                MessageBox.Show(this, "读取设备信息失败。", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }


        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Clipboard.SetText(txt.Text);
                MessageBox.Show(this, txt.Text, "已复制到剪切板。");
            }
            catch (Exception _)
            {
                MessageBox.Show(this, "复制失败。", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }
    }
}
